import 'dart:convert';
import 'package:auntserver/app_constants.dart';
import 'package:auntserver/model/entitys.dart';
import 'package:auntserver/net_api/aunt_net_help.dart';
import 'package:flustars/flustars.dart';
import 'aunt_net.dart';
import 'net_constant.dart';

///所有的数据请求接口都写在这
class AuntData {
  static getPostRequest(api, paramMap, {withLoading = false, String url}) {
    return NetManager.getInstance(baseUrl: url).post(api,
        params: NetHelp.getBaseMap(paramMap), withLoading: withLoading);
  }

  static Future requestWelcome() async {
    ResultData resultData = await getPostRequest(Api.WELCOME, null);
    // Map<String,dynamic> map = json.decode(resultData.r);
    return resultData.isSuccess
        ? WelcomeEntity.fromJson(json.decode(resultData.r))
        : false;
  }

  static Future requestAutoLogin(String accesstoken) async {
    Map<String, dynamic> map = {
      "accesstoken": accesstoken,
    };
    ResultData resultData = await getPostRequest(Api.AUTO_LOGIN, map);
    if (resultData.isSuccess) {
      AuntEntity auntEntity = AuntEntity.fromJson(json.decode(resultData.r));
      SpUtil.putObject(KeyConstant.key_userInfo, auntEntity);
      return auntEntity;
    } else {
      return false;
    }
  }

  //获取订单池列表
  static Future requestOrderPoolList(
      String userid,
      String accesstoken,
      String user_type, //类型用身份枚举下标
      String longitude, //经纬度
      String latitude //经纬度
      ) async {
    Map<String, dynamic> map = {
      "userid": userid,
      "accesstoken": accesstoken,
      "user_type": user_type,
      "longitude": longitude,
      "latitude": latitude,
    };

    ResultData resultData = await getPostRequest(Api.HOME_ORDER_LIST, map);
    return resultData.isSuccess
        ? OrderPoolEntity.fromJson(json.decode(resultData.r))
        : false;
  }

  //获取订单列表
  static Future requestOrderList(String userid, String accesstoken,
      String complete, String user_type, //类型用身份枚举下标
      {String page = "1",
      withLoading = false}) async {
    Map<String, dynamic> map = {
      "userid": userid,
      "accesstoken": accesstoken,
      "complete": complete,
      "user_type": user_type,
      "page": page,
    };
    ResultData resultData =
        await getPostRequest(Api.ORDER_LIST_NEW, map, withLoading: false);
    return resultData.isSuccess
        ? OrderEntity.fromJson(json.decode(resultData.r))
        : false;
  }

  static Future requestLogin(String phone, String password) async {
    String string = password;
    for (int i = 0; i < 3; i++) {
      string = EncryptUtil.encodeMd5(string);
    }
    Map<String, dynamic> map = {
      "phone": phone,
      "password": string,
    };
    ResultData resultData = await getPostRequest(Api.LOGIN, map);
    if (resultData.isSuccess) {
      AuntEntity auntEntity = AuntEntity.fromJson(json.decode(resultData.r));
      SpUtil.putObject(KeyConstant.key_userInfo, auntEntity);
      return auntEntity;
    } else {
      return false;
    }
  }

  static Future requestOrderDetail(String orderId, String userId,
    String userType, String type,String accessToken ,{String auntId}) async {
    Map<String, dynamic> map = {
      "userid": userId,
      "user_type": userType,
      "orderid": orderId,
      "type": type,
      "auntid": auntId,
      "accesstoken": accessToken,
    };
    //c3203fd8afa5b513c107a420ec51045c
    //5d097ca7f915c7481b5a11e3f51dd784
    ResultData resultData = await getPostRequest(Api.ORDER_DETAIL, map);
    return resultData.isSuccess?OrderDetailEntity.fromJson(json.decode(resultData.r)):false;

  }
}
